package com.ado.agea.util;

import java.util.Map;


public class SqlCreator {

	public static String crearInsert(Map<String, Map> datos, String tabla) {
		if (datos.isEmpty()){
			return "mapa " + tabla +" vacio";
		}
		
		int cont = 0;
		boolean cabecera = true;
		StringBuilder sb;
		sb = new StringBuilder();
		
		for (String key : datos.keySet()) {
			Map<String,String> fila = datos.get(key);
			if (cabecera){
				crearCabecera(sb,fila,tabla);
				cabecera=false;
			}
			sb.append("\n(");
			for(String clave : fila.keySet()){
				sb.append((fila.get(clave)==null)?" NULL,":" '" + fila.get(clave).replace("'", "''") +"',");
			}
			sb.deleteCharAt(sb.length() - 1);
			sb.append("),");
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append(";");
		return sb.toString();
	}

	private static void crearCabecera(StringBuilder sb, Map<String, String> fila, String tabla) {
		sb.append("\nINSERT INTO `" + tabla + "` (");
		for(String columna : fila.keySet()){
			sb.append("`" + columna+ "`,");
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(") VALUES ");
	}
	
}
